from multiprocessing.dummy import Pool as ThreadPool
import time
from xml.parsers.expat import ParserCreate

import requests
import xml.etree.ElementTree as ET

from ProvinceObj import ProvinceObj
from db.ProvinceDao import insertProvinces
from db.ZipCodeDao import insertZipCodes

url = 'http://www.ip138.com'
urls = []
zipCodes = []


class ZipCodeObj:
    def __init__(self, ):
        self.district = ''
        self.zipCode = ''
        self.areaCode = ''


def requestZipCode(murl):
    content = requests.get(murl).content.decode('gbk')
    # print(content)

    tag1 = 'class="t12">'
    start = content.find('class="t12">') + len(tag1)
    content = content[start:len(content)]
    start = content.find('</table>')
    content = ('%s%s%s' % ('<root>', content[0:start], '</root>'))
    parseZipCode(content)


def parseZipCode(content):  # 解析邮政编码
    content = content.replace('\n', '')
    content = content.replace('\r', '')
    content = content.replace('		', '')
    content = content.replace('®', '')
    content = content.replace('"', '\"')
    content = content.replace('%', '%%')
    content = content.replace('<td colspan=\"3\">&nbsp;</td>', '')
    content = content.replace('<td colspan=\"6\">&nbsp;</td>', '')
    content = content.replace('&nbsp;', '')
    content = content.replace('align=center', '')
    content = content.replace('</a></b>', '</b></a>')

    handler = DefaultSaxHandler()
    parser = ParserCreate()
    parser.StartElementHandler = handler.start_element
    parser.EndElementHandler = handler.end_element
    parser.CharacterDataHandler = handler.char_data
    parser.Parse(content)

    insertZipCodes(zipCodes)

    for obj in zipCodes:
        print("----------------")
        print('district', obj.district)
        print('zipCode', obj.zipCode)
        print('areaCode', obj.areaCode)


filters = ['市、县、区名', '邮政编码', '长途区号']


class DefaultSaxHandler(object):
    def __init__(self):
        self.zipCodeObj = None

    def start_element(self, name, attrs):
        # print('sax:start_element: %s, attrs: %s' % (name, str(attrs)))
        pass

        # for obj in zipCodes:
        #     print("----------------")
        #     print('district', obj.district)
        #     print('zipCode', obj.zipCode)
        #     print('areaCode', obj.areaCode)

    def end_element(self, name):
        # print('sax:end_element: %s' % name)
        pass

    def char_data(self, text):

        if text in filters or len(text) == 0:
            pass
        else:
            print('sax:char_data: %s' % text)
            if self.zipCodeObj is None:
                self.zipCodeObj = ZipCodeObj()

            # print("%d-%d-%d" % (len(self.zipCodeObj.district), len(self.zipCodeObj.zipCode), len(self.zipCodeObj.areaCode)))

            if len(self.zipCodeObj.district) == 0:
                self.zipCodeObj.district = text
            elif len(self.zipCodeObj.zipCode) == 0:
                self.zipCodeObj.zipCode = text
            elif len(self.zipCodeObj.areaCode) == 0:
                self.zipCodeObj.areaCode = text
                zipCodes.append(self.zipCodeObj)
                self.zipCodeObj = None


def getZipCode(numbers):
    urls.clear()
    for number in numbers:
        urls.append(url + number)
    print(urls)
    pool = ThreadPool(10)
    try:
        results = pool.map(requestZipCode, urls)
    except Exception as e:
        print('ConnectionError')
        print(e)
        results = pool.map(requestZipCode, urls)

    pool.close()
    pool.join()
